package pakete2;

import javax.swing.JPanel;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JComboBox;


import java.awt.event.ItemEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Vector;
import java.awt.Point;

public class EI_TxartelDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private JPasswordField jPasswordField = null;

	private JTextField jTextField1 = null;

	private JComboBox jComboBox = null;
	
	private JDialog bisitari ;

	private JLabel jLabel3 = null;
	
	private EI_SegurtasunArduraduna aita;

	String prof;
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(269, 200);
		this.setLocation(new Point(400, 250));
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(43, 3, 218, 26));
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(45, 101, 43, 24));
			jLabel2.setText("Profila:");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(56, 36, 34, 24));
			jLabel1.setText("Izena:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(33, 67, 66, 24));
			jLabel.setText("Pasahitza:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
			jContentPane.add(getJPasswordField(), null);
			jContentPane.add(getJTextField1(), null);
			jContentPane.add(getJComboBox(), null);
			jContentPane.add(jLabel3, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(42, 141, 91, 22));
			jButton.setText("Ezeztatu");
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(142, 141, 91, 22));
			jButton1.setText("Sortu");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						//lehenengo egiaztatu atal guztiak beteta daudela
						if (jPasswordField.getPassword().equals("")) {
							jLabel3.setText("Pasahitza falta da!"); //errorea bistaratu, id falta da!
						}
						if (jTextField1.getText().equals("")) jLabel3.setText(jLabel3.getText()+" Izena falta da");//errore, izena falta da!
						KudeatzaileInterfazea kud = (KudeatzaileInterfazea)Naming.lookup("rmi://localhost/kudeatzailea");
						
						//Erabiltzaile berria DBra sartu , j aldagaian erabiltzaileId gordetzen da
						int j=kud.emanDatuak(jTextField1.getText(), new String(jPasswordField.getPassword()));
						//Profila esleitu
						prof = jComboBox.getSelectedItem().toString();
//						System.out.println("Profila:"+prof);
						//i aldagaian prof profil motari dagokion profilId zenbakia gordetzen da 
						if (prof.equals("Ikaslea"))	kud.profilaEsleitu(1, j);
						if (prof.equals("Irakaslea"))	kud.profilaEsleitu(2, j);
						if (prof.equals("Langilea"))	kud.profilaEsleitu(3, j);
						if (prof.equals("Bisitaria"))	kud.profilaEsleitu(4, j);
						
						//Txartel berri bat sortu eta Esleitu Erabiltzaileari
						kud.txartelaEsleitu(j);
						JOptionPane.showMessageDialog(null, jTextField1.getText()+" erabiltzailea sortu da");
						setVisible(false);
						aita.eguneratu();
						
					} catch (MalformedURLException e1) {
						e1.printStackTrace();
					} catch (RemoteException e1) {
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						e1.printStackTrace();
					}
				}				
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JPasswordField getJPasswordField() {
		//ahal dala zenbakiak sartzen bakarrik utzi beharko luke
		if (jPasswordField== null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(104, 68, 95, 24));
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(105, 35, 95, 24));
		}
		return jTextField1;
	}
	
	public EI_TxartelDialog(EI_SegurtasunArduraduna aita) {
		super(aita,true);
		this.aita = aita;
		bisitari = new EI_BisitariaTxartela(aita);
		initialize();
		}
	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		Vector<String> profilOsagaiak = new Vector<String>();
		if (jComboBox == null) {
			profilOsagaiak.addElement("Irakaslea");
			profilOsagaiak.addElement("Ikaslea");
			profilOsagaiak.addElement("Bisitaria");
			jComboBox = new JComboBox(profilOsagaiak);
			jComboBox.setBounds(new Rectangle(109, 104, 96, 21));
			jComboBox.setEditable(false);
			jComboBox.setEnabled(true);
			jComboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED &&
				e.getItem().equals("Bisitaria") ){
				bisitari.setVisible(true);
				bisitari.pack();
				}
				}
				});
		}
		return jComboBox;
	}

}  //  @jve:decl-index=0:visual-constraint="-25,11"



